// import * as types from '@/store/mutation-type.js'
import * as CONSTANTS from '@/constants/index'
import REQUEST from '@/api/index.js'
import ROUTER from '@/router/index'
// import STORE from '@/store/index'
import { API_BASEPREFIX } from '@/constants/index'
import { Toast } from 'vant'
import { getSlashDate, getGrowAge } from '@/utils/index'

export default {
  /**
   * 异步获取评测结果
   */
  [CONSTANTS.DISPATCH_GETASYNC_HISTORYLIST]: async (
    { state, commit },
    payload
  ) => {
    try {
      commit(CONSTANTS.COMMIT_UPDATE_HISTORYLIST_ISLOADING, true)
      const { user, quiz } = state
      const { currentBabyData } = user
      const { birthday } = currentBabyData
      const { historyList } = quiz
      const { babyId, pageNo = 1, pageSize = 10 } = payload
      
      if (!babyId) return Toast.fail('LACK OF BABYID')
      if (historyList.totalPage * 1 < pageNo * 1)
        return commit(CONSTANTS.COMMIT_UPDATE_HISTORYLIST_ISLOADING, false)
      
      const params = { babyId, pageNo, pageSize }
      const { data } = await REQUEST[CONSTANTS.API_GET_HISTORYLIST](params)
      if (!data.success) throw new Error(data.info)
      const { list, total, totalPage } = data.data
      const convertList = list.map((item) => {
        const { createTime } = item
        let date = new Date(`${createTime}`.replace(/-/g, '/'))
        let slashDate = getSlashDate(date) //获取到:2020/09/08格式的日期
        let ageStage = getGrowAge(birthday, date) //获取到:1岁3个月的年龄段
        let timestamp = date.getTime(date)
        timestamp = timestamp / 1000
        return { ...item, timestamp, slashDate, ageStage }
      })
      
      let lastestItem
      if (pageNo * 1 === 1) lastestItem = convertList[0]
      if (pageNo * 1 === 1)
        commit(CONSTANTS.COMMIT_UPDATE_HISTORYLIST_LASTESTITEM, lastestItem)
      
      pageNo * 1 === 1
        ? commit(CONSTANTS.COMMIT_UPDATE_HISTORYLIST_LIST, convertList)
        : commit(CONSTANTS.COMMIT_UPDATE_HISTORYLIST_TAILLIST, convertList)

      commit(
        CONSTANTS.COMMIT_UPDATE_HISTORYLIST_PAGENO,
        data.data.pageNo * 1 + 1
      )
      commit(CONSTANTS.COMMIT_UPDATE_HISTORYLIST_TOTAL, total)
      if(totalPage*1) commit(CONSTANTS.COMMIT_UPDATE_HISTORYLIST_TOTALPAGE, totalPage)

      commit(CONSTANTS.COMMIT_UPDATE_HISTORYLIST_ISLOADING, false)
    } catch (err) {
      console.log(err)
      Toast.fail(err.message)
    }
  },
  /**
   * 上传评测结果
   */
  [CONSTANTS.DISPATCH_POST_QUIZRESULT]: async ({ state, commit }, payload) => {
    try {
      const { user, quiz } = state
      const { currentBabyId } = user
      const {
        role,
        introspectionScore,
        languageScore,
        logicScore,
        musicScore,
        naturalScore,
        relationshipScore,
        spaceScore,
        sportsScore,
      } = quiz
      const params = {
        babyId: currentBabyId,
        role,
        introspectionScore,
        languageScore,
        logicScore,
        musicScore,
        naturalScore,
        relationshipScore,
        spaceScore,
        sportsScore,
      }
      if (!currentBabyId) throw new Error('LACK OF BABYID')
      const { data } = await REQUEST[CONSTANTS.API_POST_QUIZRESULT](params)

      return data;
    } catch (err) {
      console.log(err)
      Toast.fail(err.message)
    }
  },
  /**
   * 获取宝宝当天评测的次数
   */
  [CONSTANTS.DISPATCH_GET_BABYASSESSCOUNT]: async (
    { state, commit },
    payload
  ) => {
    try {
      const { user } = state
      const { currentBabyId } = user
      const params = { babyId: currentBabyId }
      if (!currentBabyId) throw new Error('LACK OF BABYID')
      const { data } = await REQUEST[CONSTANTS.API_GET_BABYASSESSCOUNT](params)
      if (!data.success) throw new Error(data.info)
      const resData = data.data
      commit(CONSTANTS.COMMIT_UPDATE_COUNT, resData)
      return resData
    } catch (err) {
      console.log(err)
      Toast.fail(err.message)
    }
  },
  /**
   * 根据评测记录id获取评测结果
   */
  [CONSTANTS.DISPATCH_GET_RESULTBYITEMID]: async (
    { state, commit },
    payload
  ) => {
    try {
      // const { user } = state
      // const { currentBabyId } = user
      const { recordId } = payload
      const params = { recordId }
      // if (!currentBabyId) throw new Error('LACK OF BABYID')
      const { data } = await REQUEST[CONSTANTS.API_GET_RESULTBYITEMID](params)
      if (!data.success) throw new Error(data.info)
      const resData = data.data
      console.warn('resData')
      console.log(resData)
      console.log('\n')

      commit(CONSTANTS.COMMIT_UPDATE_SHAREITEM, resData)
      return resData
    } catch (err) {
      console.log(err)
      Toast.fail(err.message)
    }
  },
}
